<?php
/**
 * IndexController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';
class ProductController extends Thaind_Controller_Action
{
	var $_arrParam;
	public function init()
    {
//    	Truyền các tham số nhận được ra view
    	$this->_arrParam = $this->_request->getParams();
    	$this->view->arrParam = $this->_arrParam;
    	if (!isset($this->_arrParam['page']) || !$this->_arrParam['page']) {
    		$this->_arrParam['page'] = 1;
    	}
    	if (!isset($this->_arrParam['line_per_page']) || !$this->_arrParam['line_per_page']) {
    		$this->_arrParam['line_per_page'] = 6;
    	}
    }

    public function preDispatch(){
		$template_path = TEMPLATE_PATH . "/wap/default/";
		$this->loadTemplate($template_path,'template.ini','template');
		
		Zend_Loader::loadClass("Wap_Models_CategoryModel");
		$category_model = new Wap_Models_CategoryModel();
		$category_list = $category_model->getCategoryList();
		$this->view->arrParam['category_list'] = $category_list;
    }
    
    public function indexAction(){
    	$this->view->headLink()->appendStylesheet("/css/idangerous.swiper.css",'screen');
    	$this->view->headLink()->appendStylesheet("/css/idangerous.swiper.scrollbar.css",'screen');
    	$this->view->headScript()->appendFile("/js/idangerous.swiper.js",'text/javascript');
    	$this->view->headScript()->appendFile("/js/idangerous.swiper.scrollbar.js",'text/javascript');
		
		Zend_Loader::loadClass("Wap_Models_ProductModel");
		Zend_Loader::loadClass("Wap_Models_BannerModel");
		$product_model = new Wap_Models_ProductModel();
		$product_list = $product_model->getProductList($this->_arrParam);
		$banner_model = new Wap_Models_BannerModel();
		$banner_list = $banner_model->getBannerList(5);
		
		$this->view->product_list = $product_list;	
		$this->view->banner_list = $banner_list;	
		$this->view->arrParam['page_title'] = "Đề xuất cho bạn";
		switch ($this->_arrParam['order']) {
			case "download":
				$this->view->arrParam['page_category'] = "Tải nhiều";
				break;
			case "vote":
				$this->view->arrParam['page_category'] = "Hot nhất";
				break;
			default:
				$this->view->arrParam['page_category'] = "Mới nhất";
				break;
		}
    }
    public function moreAction(){
    	$this->_helper->layout()->disableLayout();
		Zend_Loader::loadClass("Wap_Models_ProductModel");
		Zend_Loader::loadClass("Wap_Models_BannerModel");
		$product_model = new Wap_Models_ProductModel();
		$product_list = $product_model->getProductList($this->_arrParam);
		
		$this->view->product_list = $product_list;	
		if ($product_list){
			$banner_model = new Wap_Models_BannerModel();
			$banner_info = $banner_model->getBanner();
			$this->view->banner_info = $banner_info;
		}
    }
    public function getAdvertisingAction(){
    	$this->_helper->layout()->disableLayout();
    	
    }
    public function detailAction(){
    	$this->view->headLink()->appendStylesheet("/css/idangerous.swiper.css",'screen');
    	$this->view->headLink()->appendStylesheet("/css/idangerous.swiper.scrollbar.css",'screen');
    	$this->view->headScript()->appendFile("/js/idangerous.swiper.js",'text/javascript');
    	$this->view->headScript()->appendFile("/js/idangerous.swiper.scrollbar.js",'text/javascript');
    	
    	$this->view->headScript()->appendFile("/js/fancyzoom/shadow.js",'text/javascript');
        $this->view->headScript()->appendFile("/js/fancyzoom/fancyzoom.js",'text/javascript');
        $this->view->headScript()->appendFile("/js/jvote.js",'text/javascript');
		
		Zend_Loader::loadClass("Wap_Models_ProductModel");
		Zend_Loader::loadClass("Wap_Models_SuggestionModel");
		$suggestion_model = new Wap_Models_SuggestionModel();
		$product_model = new Wap_Models_ProductModel();
		$where = $product_model->getDefaultAdapter()->quoteInto("id = ?",$this->_arrParam['id']);
		$client = new Zend_Http_Client("https://graph.facebook.com/?ids=" . HOMEPAGE . "/product/detail/id/" . $this->_arrParam['id']);
		$body = Zend_Json::decode($client->request()->getBody());
		$data = array("comment"	=> (int) @$body[HOMEPAGE . "/product/detail/id/" . $this->_arrParam['id']]['comments'],
//						"like"	=> (int) @$body[HOMEPAGE . "/product/detail/id/" . $this->_arrParam['id']]['shares']
		);
		$product_model->update($data, $where);
		$product_info = $product_model->getProductInfo($this->_arrParam["id"]);
		
		if($product_info){
			$this->view->product_info = $product_info;	
			$this->view->relative_product_list = $suggestion_model->getSuggestionList();
			$this->view->arrParam['page_category'] = $product_info['category'];
		}else{
			$this->_redirect("/wap/product");
		}
    }
    public function reportErrorAction(){
    	$mail = new Zend_Mail ('UTF-8');					
//					$mail->setFrom('dinhthaiptit@gmail.vn',"Storegame");		
		$html = "Lỗi : <a href = " . HOMEPAGE . "'/product/detail/id/" . $this->_arrParam['id'] . "'>Link lỗi</a>";
		$mail->setBodyHtml($html);
		$mail->addTo("nguyendinhthai1986@gmail.com");					
		
		$mail->setSubject("Lỗi game");
		$mail->send ();
		$this->_redirect("/product/detail/id/" . $this->_arrParam['id']);
    }
    public function voteAction(){
    	$this->_helper->viewRenderer->setNoRender();
    	$this->_helper->layout()->disableLayout();
    	Zend_Loader::loadClass("Wap_Models_ProductModel");
    	$product_model = new Wap_Models_ProductModel();
    	$where = $product_model->getDefaultAdapter()->quoteInto("id = ?",$this->_arrParam['id']);
    	$data = array("vote"			=> new Zend_Db_Expr("vote + 1"),
    					"vote_value"	=> new Zend_Db_Expr("vote_value + " . $this->_arrParam['vote-value'])
    	);
    	$product_model->update($data, $where);
    }
    public function downloadAction(){
    	$this->_helper->viewRenderer->setNoRender();
    	Zend_Loader::loadClass("Wap_Models_ProductModel");
		$product_model = new Wap_Models_ProductModel();
		$product_info = $product_model->getProductInfo($this->_arrParam["id"]);
		$download_link_list = Zend_Json::decode($product_info['download_link']);
		if (isset($download_link_list[$this->_arrParam['key']])){
			$where = $product_model->getDefaultAdapter()->quoteInto("id = ?",$this->_arrParam['id']);
			$product_model->update(array("download"	=> new Zend_Db_Expr("download + 1")), $where);
			$this->_redirect($download_link_list[$this->_arrParam['key']]);
		}else{
			$this->_redirect("/product/detail/id/" . $this->_arrParam['id']);
		}
    }
}
